#include <iostream>
#include <map>
#include <functional>

class Product {
public:
    virtual void display() = 0;
    virtual ~Product() {}
};

class ProductA : public Product {
public:
    void display() override { std::cout << "Product A" << std::endl; }
};

class ProductB : public Product {
public:
    void display() override { std::cout << "Product B" << std::endl; }
};

class Factory {
private:
    std::map<std::string, std::function<Product*()>> registry;

public:
    void registerProduct(const std::string& key, std::function<Product*()> creator) {
        registry[key] = creator;
    }

    Product* createProduct(const std::string& key) {
        if (registry.find(key) != registry.end()) {
            return registry[key]();
        }
        return nullptr;
    }
};

int main() {
    Factory factory;
    factory.registerProduct("A", []() -> Product* { return new ProductA(); });
    factory.registerProduct("B", []() -> Product* { return new ProductB(); });

    Product* productA = factory.createProduct("A");
    if (productA != nullptr) {
        productA->display();
    }

    Product* productB = factory.createProduct("B");
    if (productB != nullptr) {
        productB->display();
    }

    delete productA;
    delete productB;

    return 0;
}